<template>
  <div class="editInfo">
    <Header>
      <slot> 编辑地址 </slot>
    </Header>
    <section>
      <van-address-edit
        :area-list="areaList"
        show-delete
        show-set-default
        show-search-result
        @delete="onDelete"
        @save="onSave"
        :address-info="AddressInfo"
      />
    </section>
    <Tabbar></Tabbar>
  </div>
</template>

<script>
import Header from "@/components/Path/Header.vue";
import Tabbar from "@/components/Tabbar.vue";
import http from "@/common/request";
import { Toast } from "vant";
export default {
  components: { Header, Tabbar },
  data() {
    return {
      areaList: {
        province_list: {
          110000: "北京市",
          120000: "天津市",
        },
        city_list: {
          110100: "北京市",
          120100: "天津市",
        },
        county_list: {
          110101: "东城区",
          110102: "西城区",
          120101: "塘沽区",
          // ....
        },
      },
      address: {},
      AddressInfo: {},
    };
  },
  methods: {
    // 点击保存
    async onSave(content) {
      content.isDefault = content.isDefault ? 1 : 0;
      let res = await http.$axios({
        method: "post",
        url: "/api/updateAddress",
        headers: {
          token: true,
        },
        data: {
          content,
        },
      });
      // console.log(res);
      if (res.success) {
        Toast("修改成功");
        this.$router.push({
          path: "/path",
        });
      }
    },
    // 点击删除
    async onDelete(item) {
      // Toast("delete");
      // 获取地址id
      let id = item.id;
      let res = await http.$axios({
        method: "post",
        url: "/api/deleteAddress",
        headers: {
          token: true,
        },
        data: {
          id,
        },
      });
      console.log(res);
      if (res.success) {
        Toast(res.msg);
        this.$router.push({
          path: "/path",
        });
      }
      // console.log(item);
    },
  },
  mounted() {
    this.$route.params.isDefault =
      this.$route.params.isDefault == 1 ? true : false;
    this.AddressInfo = this.$route.params;
    console.log(this.address);
  },
};
</script>

<style scoped>
.editInfo {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100vw;
}
section {
  flex: 1;
  overflow: hidden;
}
.tabbar {
  border-top: 2px solid #aaa;
}
Header {
  font-size: 18px;
}
::v-deep .van-button {
  background-color: rgba(210, 0, 0, 1);
}
::v-deep .van-button {
  color: aliceblue;
}
</style>